package servlet;

import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class orders_Delete extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public orders_Delete() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	//删除订单信息
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		PrintWriter out = response.getWriter();

		conn cn = new conn();
		String f=request.getParameter("f");

        //得到要删除的ID
        String id = request.getParameter("id");


		
		if (f==null)
        {
			//设置删除sql
			String sql="delete from orders where no = '"+id+"' ";
			



			int i=cn.update(sql);

			cn.update("delete from ordermore where no='"+id+"' ");

			out.println("<script>");
			out.println("alert('操作成功');");
			out.println("window.location='"+request.getHeader("Referer")+"'");
			out.println("</script>");
        }
		else if(f.equals("1"))
		{
			String sql="update orders set oflag='已发货' where no = '"+id+"' ";




			int i=cn.update(sql);

			cn.update("insert into orderstatus(no, omemo, otime) VALUES('"+id+"','商家已发货',now()) ");

			out.println("<script>");
			out.println("alert('操作成功');");
			out.println("window.location='"+request.getHeader("Referer")+"'");
			out.println("</script>");
		}
		else  if(f.equals("2"))
		{
			String sql="update orders set oflag='已完成' where no = '"+id+"' ";



			int i=cn.update(sql);

			cn.update("insert into orderstatus(no, omemo, otime) VALUES('"+id+"','买家已确认收货',now()) ");

			JSONArray jsonarray = new JSONArray();
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("jieguo", 1);
			jsonarray.add(jsonobj);
			out = response.getWriter();
			out.println(jsonarray);
		}
        else if(f.equals("3"))
		{
			String sql="update orders set flag=3 where no = '"+id+"' ";


			int i=cn.update(sql);



			JSONArray jsonarray = new JSONArray();
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("jieguo", 1);
			jsonarray.add(jsonobj);
			out = response.getWriter();
			out.println(jsonarray);
		}

		out.flush();
		out.close();
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}


	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

